﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyCommerce.Presentation.UsersAndSecurity.IViews;
using Innova.Microfinanzas.BusinessEntities;
using Innova.FoundationFramework.DataTypes;
using Innova.Microfinanzas.Business.UsersAndSecurity;
using Innova.Microfinanzas.BusinessEntities.SystemEnums;

namespace MyCommerce.Presentation.UsersAndSecurity.Presenters
{
    public class RoleFillPresenter
    {
        private IRoleFillView pViewToPresent;

        public RoleFillPresenter(IRoleFillView vViewToPresent)
        {
            pViewToPresent = vViewToPresent;
            pViewToPresent.OnCancel += new EventHandler(pViewToPresent_OnCancel);
            pViewToPresent.OnLoadView += new GenericEventHandler<Role>(pViewToPresent_OnLoadView);
            pViewToPresent.OnSave += new EventHandler(pViewToPresent_OnSave);
        }

        private void pViewToPresent_OnSave(object sender, EventArgs e)
        {
            try
            {
                RoleManager userMgr = new RoleManager();
                Role objUserToSave = pViewToPresent.Get();
                if (pViewToPresent.OptionViewSelect == OptionSelect.Create)
                    userMgr.CreateRole(objUserToSave);
                else
                {
                    objUserToSave.IdRole = pViewToPresent.IdItemEdit;
                    userMgr.UpdateRole(objUserToSave);
                }
                pViewToPresent.CloseAndSetResult(true);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void pViewToPresent_OnLoadView(object sender, GenericEventArgs<Role> e)
        {
            if (e.EventData != null)
                pViewToPresent.Set(e.EventData);
        }

        private void pViewToPresent_OnCancel(object sender, EventArgs e)
        {
            pViewToPresent.CloseAndSetResult(false);
        }
    }
}
